package com.example.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.pojo.Xianlu;
import com.example.service.GantaService;
import com.example.service.XianLuService;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/xianlu")
public class XianLuController {
	@Autowired
	XianLuService service;

	@Autowired
	GantaService gtservice;

	@RequestMapping("/findallxl")
	public PageInfo<Xianlu> findall(Integer pageNum,Integer pageSize) {
		System.out.println(pageNum+"---"+pageSize);
		  if(pageNum==null) pageNum=1; 
		  if(pageSize==null) pageSize=8; 
		  PageInfo<Xianlu> pager = new PageInfo<Xianlu>(service.findall(pageNum,pageSize));
		return pager;
		 
	}

	
	/*
	 * @RequestMapping("/xlall") public PageInfo<Xianlu> findall(Integer
	 * pageNum,Integer pageSize){ System.out.println(pageNum+"---"+pageSize);
	 * if(pageNum==null) pageNum=1; if(pageSize==null) pageSize=8; PageInfo<Xianlu>
	 * pager = new PageInfo<Xianlu>(service.findall(pageNum,pageSize)); return
	 * pager; }
	 */
	 
	/*
	 * @RequestMapping("/xlidall") public Set findAllId() { Set list=new HashSet();
	 * for(int i=0;i<service.findall().size();i++) {
	 * list.add(service.findall().get(i).getXlld()); } return list; }
	 */

	/*
	 * @RequestMapping("/findxianluall") public List<Xianlu> findxianluAll(){ return
	 * service.findxianluAll2(); }
	 */
	/*
	 * @RequestMapping("/findxianluall") public List<Xianlu> findxianluAll(){ return
	 * null;
	 * 
	 * }
	 */
	private List<Xianlu> pager() {
		// TODO Auto-generated method stub
		return null;
	}


	@RequestMapping("/xlfindbyid")
	public PageInfo<Xianlu> findbyid(String xlid, String xlname, int isqy, Integer pageNum, Integer pageSize) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 8;
		PageInfo<Xianlu> pager = new PageInfo<Xianlu>(service.findall2(xlid, xlname, isqy, pageNum, pageSize));
		return pager;
	}

	@RequestMapping("/xianlufindByxlldAndXlname")
	public Xianlu findByxlldAndXlname(String xlid, String xlname) {
		System.out.println(xlid + xlname);
		return service.findByxlldAndXlname(xlid, xlname);
	}

	@RequestMapping("/addxl")
	public String addxl(@RequestBody Xianlu xl) {
		// xl.setFDate(null);
		System.out.println(xl);
		String str = "添加成功！";
		if (xl.getGantas() != null) {
			System.out.println("不为空");
			System.out.println(xl.getGantas());
			for (int i = 0; i < xl.getGantas().size(); i++) {
				gtservice.updateGantaXlld(xl.getXlld(), xl.getGantas().get(i).getGid());
			}
			str += "添加杆塔" + xl.getGantas().size() + "根";
		}
		xl.setIsqy(1);
		service.add(xl);
		return str;
	}

	@RequestMapping("/updatexianluandganta")
	public String updatexianluandganta(@RequestBody Xianlu xl) {
		// xl.setFDate(null);
		System.out.println(xl);
		String str = "修改成功！";
		service.add(xl);
		/*
		 * if(xl.getGantas()!=null || xl.getGantas().size()>0) {
		 * System.out.println("------------"); for(int i=0;i<xl.getGantas().size();i++)
		 * { gtservice.addGanTa(xl.getGantas().get(i)); }
		 * str+="移除了"+xl.getGantas().size()+"根杆塔"; }
		 */
		return str;
	}

	@RequestMapping("/updatexianlu")
	public String update(int is, int id) {
		service.update(is, id);
		gtservice.UpdateIsqy(is, id);
		return "OK";
	}

	@RequestMapping("/deleteXLByxlld")
	public void deleteXL(String xlld) {
		service.deleteXL(xlld);
	}

	@RequestMapping("/updateGantaisXlld")
	public void deleteXL(String xlld, String str) {
		String[] strall = str.split(",");
		List<Integer> gids = new ArrayList<Integer>();
		if (strall.length > 0 && strall[0] != "") {
			for (String st : strall) {
				gids.add(Integer.parseInt(st));
			}
		}
		service.updateGanTaisGidAndXlld(xlld, gids);
	}

}
